//
//  TEHTTPRequestViewController.m
//  TintExample
//
//  Created by Kevin Wu on 2/29/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEHTTPRequestViewController.h"

@implementation TEHTTPRequestViewController

- (void)dealloc {
    TTRelease(_request);
    [super dealloc];
}


#pragma mark - UIViewController

- (void)loadView {
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addButtonWithTitle:@"Load small" selector:@selector(beginLoadingSmall:)];
    [self addButtonWithTitle:@"Load big" selector:@selector(beginLoadingBig:)];
    [self addButtonWithTitle:@"Load bad url" selector:@selector(beginLoadingBadURL:)];
    [self addButtonWithTitle:@"Load unreachable url" selector:@selector(beginLoadingUnreachableURL:)];
    [self addButtonWithTitle:@"Cancel" selector:@selector(cancelLoading:)];
}

- (void)beginLoadingSmall:(id)sender {
    TTRelease(_request);
    _request = [[TTHTTPRequest alloc] initWithURLAddress:@"http://www.baidu.com/"];
    _request.destinationPath = [TTDocumentsPath() stringByAppendingPathComponent:@"a.txt"];
    [_request addDelegate:self];
    [_request startAsynchronous];
}

- (void)beginLoadingBig:(id)sender {
    TTRelease(_request);
    _request = [[TTHTTPRequest alloc] initWithURLAddress:@"http://farm8.staticflickr.com/7190/6928533495_349caba974_o.jpg"];
    //_request.destinationPath = [TTDocumentsPath() stringByAppendingPathComponent:@"b.jpg"];
    _request.showAccurateProgress = YES;
    [_request addDelegate:self];
    [_request startAsynchronous];
}

- (void)beginLoadingBadURL:(id)sender {
    TTRelease(_request);
    _request = [[TTHTTPRequest alloc] initWithURLAddress:@"blah blah"];
    [_request addDelegate:self];
    [_request startAsynchronous];
}

- (void)beginLoadingUnreachableURL:(id)sender {
    TTRelease(_request);
    _request = [[TTHTTPRequest alloc] initWithURLAddress:@"http://www.facebook.com/"];
    [_request addDelegate:self];
    [_request startAsynchronous];
}


- (void)cancelLoading:(id)sender {
    [_request cancel];
}


- (void)operationDidStart:(TTOperation *)operation {
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    TTDPRINT(@"REQUEST: %@", request.URLAddress);
}
- (void)operationUpdated:(TTOperation *)operation {
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    TTDPRINT(@"REQUEST: %@ %u/%u", request.URLAddress, request.totalBytesRead, request.contentLength);
}
- (void)operationDidFail:(TTOperation *)operation withError:(NSError *)error {
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    TTDPRINT(@"REQUEST: %@", request.URLAddress);
}
- (void)operationWillFinish:(TTOperation *)operation {
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    TTDPRINT(@"REQUEST: %@", request.URLAddress);
}
- (void)operationDidFinish:(TTOperation *)operation {
    TTHTTPRequest *request = (TTHTTPRequest *)operation;
    TTDPRINT(@"REQUEST: %f KB %@", [request.responseData length]/1024.0, request.URLAddress);
}

@end
